package com.all.diy.utils;
import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import com.all.diy.R;
import com.lxj.xpopup.core.BottomPopupView;
public class CustomPopup extends BottomPopupView {
    private final PopupInteractionListener listener;
    private EditText textView;
    private SwitchCompat aSwitch;
    private String deviceId = "";

    public CustomPopup(@NonNull Context context, PopupInteractionListener listener,String deviceId) {
        super(context);
        this.listener = listener;
        this.deviceId= deviceId;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_popup; // 绑定布局
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        textView =  findViewById(R.id.machine_id);
        aSwitch =  findViewById(R.id.switch_button);
        textView.setText(deviceId);
        int cursorPosition = textView.length(); // 获取文本长度即为光标位置
        textView.setSelection(cursorPosition);
        findViewById(R.id.custom_pop_save).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String machine_id =  textView.getText().toString();
                boolean isChecked = aSwitch.isChecked();
                onConfirmClick(machine_id,isChecked);
            }
        });
    }

    private void onConfirmClick(String machine_id,boolean isChecked) {
        if (listener != null) {
            listener.onConfirmClick(machine_id,isChecked);
        }
        dismiss();
    }

}


